package com.example.userprofile

import androidx.lifecycle.*
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

//@HiltViewModel
class UserProfileViewModel /*@Inject constructor*/(
    savedStateHandle: SavedStateHandle
//    userRepository: UserRepository
): ViewModel() {
    //private val userId : String = savedStateHandle["uid"] ?: "123"
    //throw java.lang.IllegalArgumentException("missing user id")

    /*
    https://developer.android.google.cn/jetpack/guide
    LiveData is an observable data holder. Other components in your app can monitor changes to objects using this holder without creating explicit and rigid dependency paths between them.
    The LiveData component also respects the lifecycle state of your app's components—such as activities, fragments, and services—and includes cleanup logic to prevent object leaking and excessive memory consumption.

    LiveData is an observable data holder., the UserProfileFragment is informed when the data is updated.
    Furthermore, because this LiveData field is lifecycle aware, it automatically cleans up references after they're no longer needed.
     */

    private val _user = MutableLiveData<User>()
    val user : LiveData<User> = _user

    //Connect ViewModel and the repository
    init {
//        viewModelScope.launch {
            //_user.value = userRepository.getUser(userId)
            _user.value = User("12345","Pao","Xu")
//        }
    }

    fun changeUser(user:User){
        _user.value = user
    }
    //androidx.room:room-ktx
//   val user : LiveData<User> = userRepository.getUser("123").asLiveData()


//    class UserProfileViewModelFactory(): ViewModelProvider.Factory{
//        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
//            return UserProfileViewModel() as T
//        }
//    }
}
